package io.demo.message.domain.proto;


import io.demo.message.domain.proto.Address.AddressType;

public class AddressMessageConverter implements
    MessageConverter<io.demo.message.domain.Address, Address> {

  @Override
  public Address toMessage(io.demo.message.domain.Address pojo) {
    Country country =
        MessageConverters.toMessage(pojo.getCountry(), io.demo.message.domain.Country.class);
    City city = MessageConverters.toMessage(pojo.getCity(), io.demo.message.domain.City.class);
    return Address.newBuilder()
        .setId(pojo.getId())
        .setAddressType(AddressType.valueOf(pojo.getAddressType().name()))
        .setCountry(country)
        .setCity(city)
        .setStreet(pojo.getStreet())
        .setZipCode(pojo.getZipCode())
        .build();
  }

  @Override
  public io.demo.message.domain.Address fromMessage(Address message) {
    return io.demo.message.domain.Address.builder()
        .id(message.getId())
        .country(
            MessageConverters
                .fromMessage(message.getCountry(), io.demo.message.domain.Country.class)
        ).city(
            MessageConverters.fromMessage(message.getCity(), io.demo.message.domain.City.class)
        ).zipCode(message.getZipCode())
        .street(message.getStreet())
        .addressType(io.demo.message.domain.AddressType.valueOf(message.getAddressType().name()))
        .build();
  }
}
